﻿package mortal.game.view.common.tooltip.tooltips
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.tooltip.tooltips.base.*;
    import mortal.game.view.medal.*;

    public class ToolTipChallengeCopy extends ToolTipScaleBg implements IWindow3D, IToolTip3D
    {
        private var _challengeInfo:TChallengeCopy;
        protected var _line3D1:ScaleBitmap;
        protected var _3dBg:GImageBitmap;
        protected var _line3D2:ScaleBitmap;
        protected var _3dObj:ActionPlayer;
        protected var _rect3D:Rect3DObject;
        protected var _img2d:Img2D;
        protected var _3dRectWidth:int = 200;
        protected var _3dRectHeight:int = 200;
        private var _3dBgx:int = 235;
        private var _3dBgy:int = 0;
        private var _bg:GImageBitmap;
        private var _bossName:GTextFiled;
        private var _des:GTextFiled;
        private var _combat:BitmapNumberText;
        private var _rewardList:Vector.<BaseItem>;
        protected var _contentTopOf3DSprite:Sprite;

        public function ToolTipChallengeCopy()
        {
            this._rewardList = new Vector.<BaseItem>;
            this._contentTopOf3DSprite = new Sprite();
            this.createChildren();
            return;
        }// end function

        protected function createChildren() : void
        {
            this.updateSize(444, 235);
            this._bg = UIFactory.gImageBitmap("", 0, 0, this);
            UIFactory.gImageBitmap(ImagesConst.challengeCopyNewTxt_3, 92, 138, this);
            UIFactory.gImageBitmap(ImagesConst.ChallengeNewSugCombatBg, 20, 100, this);
            var _loc_1:* = GlobalStyle.textFormatHuang2.center();
            _loc_1.size = 16;
            this._bossName = UIFactory.gTextField("", 9, 12, 220, 50, this, _loc_1);
            this._des = UIFactory.gTextField("", 12, 45, 220, 65, this, GlobalStyle.textFormatBai.left());
            this._des.wordWrap = true;
            this._des.multiline = true;
            this._combat = UIFactory.bitmapNumberText(105, 103, "SignUpNum2.png", 22, 23, -6, this);
            this._3dBg = UIFactory.gImageBitmap("", this._3dBgx, this._3dBgy, null, this.bgLoadedHander);
            var _loc_2:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 9, 145, this);
            var _loc_3:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 231, 145, this);
            _loc_3.scaleX = -1;
            return;
        }// end function

        public function get contentContainer() : Sprite
        {
            return this;
        }// end function

        override public function set data(param1) : void
        {
            this.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.removeFromeStageHandler);
            super.data = param1;
            this._challengeInfo = param1;
            if (this._challengeInfo.discStr)
            {
                this._des.text = this._challengeInfo.discStr;
            }
            this._bossName.text = this._challengeInfo.levelName;
            this._combat.text = this._challengeInfo.sugCombat.toString();
            if (this._challengeInfo.level == ChallengeCopyConfig.instance.getMaxLevelByTowerAndSection(this._challengeInfo.tower, this._challengeInfo.section))
            {
                this._bg.imgUrl = ImagesConst.ChallengeTipsBg_boss;
            }
            else
            {
                this._bg.imgUrl = ImagesConst.ChallengeTipsBg_monster;
            }
            this._3dBg.imgUrl = ImagesConst.ChallengeTip3Dbg;
            this.updateRewardList();
            return;
        }// end function

        private function updateRewardList() : void
        {
            var _loc_1:BaseItem = null;
            var _loc_3:Array = null;
            var _loc_4:ItemData = null;
            var _loc_6:int = 0;
            this.removeRewardItems();
            var _loc_2:* = this._challengeInfo.reward.split("&");
            var _loc_5:* = MedalConfig.instance.getTmedalByCode(this._challengeInfo.medalId);
            if (_loc_5)
            {
                _loc_1 = UICompomentPool.getUICompoment(BaseItem);
                _loc_1.setItemStyle(ItemStyleConst.Small, ImagesConst.PackItemBg, 2, 2);
                var _loc_7:Boolean = false;
                _loc_1.isDropAble = false;
                _loc_1.isDragAble = _loc_7;
                _loc_1.isShowLock = true;
                _loc_1.source = MedalUtil.getMedalIconUrlByTmedal(_loc_5, 0);
                _loc_1.setColorBg(4);
                this.addChild(_loc_1);
                this._rewardList.push(_loc_1);
            }
            while (_loc_6 < _loc_2.length)
            {
                
                if (_loc_2[_loc_6])
                {
                    _loc_3 = (_loc_2[_loc_6] as String).split("#");
                    _loc_4 = new ItemData(int(_loc_3[0]));
                    if (_loc_4.itemInfo && ItemsUtil.isFitCarrer(_loc_4))
                    {
                        _loc_1 = UICompomentPool.getUICompoment(BaseItem);
                        _loc_1.setItemStyle(ItemStyleConst.Small, ImagesConst.PackItemBg, 2, 2);
                        var _loc_7:Boolean = false;
                        _loc_1.isDropAble = false;
                        _loc_1.isDragAble = _loc_7;
                        _loc_1.isShowLock = true;
                        _loc_1.itemData = _loc_4;
                        _loc_1.amount = _loc_3[1];
                        this.addChild(_loc_1);
                        this._rewardList.push(_loc_1);
                    }
                }
                _loc_6++;
            }
            _loc_6 = 0;
            while (_loc_6 < this._rewardList.length)
            {
                
                this._rewardList[_loc_6].x = _loc_6 * 45 + (225 - 45 * this._rewardList.length) * 0.5 + 9;
                this._rewardList[_loc_6].y = 173;
                _loc_6++;
            }
            return;
        }// end function

        private function removeRewardItems() : void
        {
            var _loc_1:BaseItem = null;
            for each (_loc_1 in this._rewardList)
            {
                
                _loc_1.dispose(true);
            }
            this._rewardList.length = 0;
            return;
        }// end function

        protected function addToStageHandler(event:Event) : void
        {
            this.bgLoadedHander();
            return;
        }// end function

        protected function removeFromeStageHandler(event:Event) : void
        {
            if (this._img2d)
            {
                this._img2d.dispose(true);
                this._img2d = null;
            }
            if (this._3dObj)
            {
                this._rect3D.disposeObj3d(this._3dObj);
                this._3dObj = null;
            }
            if (this._rect3D)
            {
                Rect3DManager.instance.closeEndHander(null, this);
                this._rect3D = null;
            }
            this.dispatchEvent(new WindowEvent(WindowEvent.CLOSE_END));
            this.removeEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            this.removeEventListener(Event.REMOVED_FROM_STAGE, this.removeFromeStageHandler);
            return;
        }// end function

        public function get contentTopOf3DSprite() : Sprite
        {
            return this._contentTopOf3DSprite;
        }// end function

        private function bgLoadedHander() : void
        {
            if (!this.stage || !this._3dBg.bitmapData || this._rect3D)
            {
                return;
            }
            var _loc_1:* = new Rectangle(this._3dBgx + paddingLeft, this._3dBgy + paddingTop + 7, 201, 222);
            this._rect3D = Rect3DManager.instance.registerWindow(true, _loc_1, this, true);
            Rect3DManager.instance.windowShowHander(null, this);
            if (this._3dBg.bitmapData)
            {
                if (this._img2d)
                {
                    this._img2d.dispose(true);
                }
                this._img2d = new Img2D(null, this._3dBg.bitmapData, new Rectangle(0, 0, 201, 222), false);
                this._img2d.autoDispose = false;
                this._img2d.x = 0;
                this._img2d.y = 0;
                this._rect3D.addImg(this._img2d);
            }
            var _loc_2:* = BossConfig.instance.getInfoByCode(int(this._challengeInfo.bossId));
            var _loc_3:* = _loc_2.mesh + ".md5mesh";
            var _loc_4:* = _loc_2.bone + ".skeleton";
            if (this._3dObj)
            {
                this._rect3D.disposeObj3d(this._3dObj);
            }
            this._3dObj = FrObjectPool.getObject(ActionPlayer, this);
            if (this._3dObj.meshUrl != _loc_3 && this._3dObj.animUrl != _loc_4)
            {
                this._3dObj.load(_loc_3, _loc_4, _loc_2.texture);
            }
            FrEventDispatcher.instance.proxyAddEventListener(this._3dObj, Engine3dEventName.PARSE_MESH_FINISH, this.modleLoadedHandler);
            this._3dObj.changeAction(ActionName.Stand);
            this._3dObj.play();
            this._3dObj.rotationY = -15;
            this._3dObj.scaleValue = this._challengeInfo.bossModePercent / 100;
            this._rect3D.addObject3d(this._3dObj, this._3dRectWidth / 2, this._3dRectHeight - 20);
            this.updatePosition();
            this.dispatchEvent(new WindowEvent(WindowEvent.SHOW_END));
            return;
        }// end function

        public function updatePosition() : void
        {
            this.dispatchEvent(new WindowEvent(WindowEvent.POSITIONCHANGE));
            return;
        }// end function

        protected function modleLoadedHandler(param1 = null) : void
        {
            return;
        }// end function

    }
}
